package com.xb.loan.encryptor.util;


import java.io.File;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;

public class TestAddKey {

	public static void addKey(String path, String partnerId) {
		
		try {
			RSAKey[] keys = RSAUtils.genKeyPair();
			// 公钥保存路径
			String pubPath = path + File.separatorChar + "partner" + File.separatorChar + partnerId + File.separatorChar + "gen" + File.separatorChar + "publicKey.txt";
			// 私钥保存路径
			String priPath = path + File.separatorChar + "partner" + File.separatorChar + partnerId + File.separatorChar + "gen" + File.separatorChar + "privateKey.txt";
			// 公钥
			RSAPublicKey publicKey = (RSAPublicKey) keys[0];
			// 私钥
			RSAPrivateKey privateKey = (RSAPrivateKey) keys[1];
			// 公钥base64字符串
			String pubKeyStr = Base64Utils.encode(publicKey.getEncoded());
			// 保存公钥
			Base64Utils.byteArrayToFile(pubKeyStr.getBytes(), pubPath);
			// 保存私钥
			Base64Utils.byteArrayToFile(Base64Utils.encode(privateKey.getEncoded()).getBytes(), priPath);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidKeySpecException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	
	public static void main(String[] args) {
		TestAddKey.addKey("E:/", "10000000550");
	}

}
